//
//  ZDBSetViewController.m
//  ZDB
//
//  Created by Kk on 2020/3/17.
//  Copyright © 2020 ZDB. All rights reserved.
//

#import "ZDBSetViewController.h"
#import "SetModel.h"
#import "ZDBSetTableViewCell.h"
#import "ZDBFeedbackViewController.h"
#import "ZDContactUsBViewController.h"
#import "CheckVersionAlearView.h"
#import "AppDelegate.h"
#import "ZDBAboutUsViewController.h"
#import "LLWebViewController.h"
@interface ZDBSetViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)NSMutableArray *dataSource;
@property (nonatomic,strong)UITableView * tableView;
@end

@implementation ZDBSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
}
-(void)axcBaseRequestData
{
    NSMutableArray * firestAry = [NSMutableArray new];
    [firestAry  addObject:@{@"title":@"联系我们",@"righTitle":@"",@"controllerClass":[ZDContactUsBViewController class]}];
    [firestAry addObject:@{@"title":@"意见反馈",@"righTitle":@"",@"controllerClass":[ZDBFeedbackViewController class]}];
    NSMutableArray * secondAry = [NSMutableArray new];
    [secondAry addObject:@{@"title":@"隐私协议",@"righTitle":@"",@"controllerClass":[LLWebViewController class]}];
    [secondAry addObject:@{@"title":@"关于我们",@"righTitle":@"",@"controllerClass":@""}];
    [secondAry addObject:@{@"title":@"检查更新",@"righTitle":kFormat(@"当前版本 V%@", [ZBDUnit getVersion]),@"controllerClass":@""}];
    
    NSMutableArray * dataAry = [NSMutableArray new];
    [dataAry addObject:firestAry];
    [dataAry addObject:secondAry];
    for (NSArray * array in dataAry)
    {
        NSMutableArray * newArray = [NSMutableArray new];
        for (NSDictionary *dic in array) {
            SetModel *modle=[[SetModel alloc]init];
            modle.title=dic[@"title"];
            modle.righTitle=dic[@"righTitle"];
            modle.controllerClass=dic[@"controllerClass"];
            [newArray addObject:modle];
        }
        [self.dataSource addObject:newArray];
    }
    [self.tableView reloadData];
}
- (NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}
#pragma  mark -----懒加载
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UIScreenWidth, UIScreenHeight  - kStatusBarAndNavigationBarHeight)style:UITableViewStylePlain];
        _tableView.tableFooterView = self.footView;
        _tableView.separatorColor = CELLCOLOR;
        _tableView.delegate = self;
        _tableView.dataSource=self;
        _tableView.backgroundColor =  VIEWBGCOLOR;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 0.0001)];
        
        [_tableView registerNib:[UINib nibWithNibName:@"ZDBSetTableViewCell" bundle:nil]  forCellReuseIdentifier:@"ZDBSetTableViewCell"];
    
    }
    return _tableView;
}
- (UIView *)footView
{
    UIView * footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 88)];
    UIButton * logoutBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 40, UIScreenWidth, 48)];
    logoutBtn.backgroundColor = MAINCOLOR;
    [logoutBtn setTitle:@"退出登录" forState:0];
    logoutBtn.titleLabel.font = AutoFont(18);
    [logoutBtn setTitleColor:WHITECOLOR forState:0];
    [logoutBtn add_BtnClickHandler:^(NSInteger tag) {
        [[DJLoginHelper sharedInstance] loginOut];
        [self.navigationController popViewControllerAnimated:YES];
    }];
    [footView addSubview:logoutBtn];
    return footView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray * array = self.dataSource[section];
    return array.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 52;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 10)];
    view.backgroundColor = self.view.backgroundColor;
    return view;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZDBSetTableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"ZDBSetTableViewCell" forIndexPath:indexPath];
    cell.setModel = _dataSource[indexPath.section][indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     SetModel *si = _dataSource[indexPath.section][indexPath.row];
    switch (indexPath.section) {
        case 0:
        {
            ZBDBaseViewController *vc = [[si.controllerClass alloc] init];
            vc.navigationItem.title = si.title;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        case 1:
        {
            if (indexPath.row==0) {//隐私协议
                
                   LLWebViewController *webV = [LLWebViewController new];
                   NSString * url = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"explain_index"];
                   webV.urlStr = url;
                   webV.isPullRefresh = YES;
                   [self.navigationController pushViewController:webV animated:YES];
                
            }else if (indexPath.row==1)//关于我们
            {
                ZDBAboutUsViewController * vcc = [ZDBAboutUsViewController new];
                vcc.title = @"关于我们";
                [self.navigationController pushViewController:vcc animated:YES];
            }else if (indexPath.row==2)//检查更新
            {
                if ( [AppDelegate shareAppdelegate].isUpdata) {
                    
                     [self getVersion];
                }else
                {
                    [JMNotifyView showNotify:@"已是最新版本" isSuccessful:NO];
                }
               
               
            }
            
        }
            break;
        default:
            break;
    }
}
- (void)getVersion
{
    [self showLoadingAnimation];
    WeakSelf
    NSString  * getAppVersionByType = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"getAppVersionByType"];
    [[SmileHttpTool sharedInstance] POST:getAppVersionByType parameters:@{@"type":@"2"} origin:NO success:^(NSInteger statusCode, NSString *message, id responseObject) {
        
        NSDictionary * dic  = responseObject;
        if (kDictIsEmpty(dic)) {
            return ;
        }
        NSString * content = dic[@"content"];//更新内容
        NSString * isUpdate =dic[@"isUpdate"]; //1为建议升级，2为强制升级
        NSString *  url =  dic[@"url"]; //更新提示
        NSString *  Version = [ZBDUnit getVersion];
        NSString * version_code = dic[@"version"];
        BOOL isUpdata;
        //本地版本大于等于线上的，不需要提示更新，如果本地版本小于线上的，就需要提示更新
        if ([[Version stringByReplacingOccurrencesOfString:@"." withString:@""] floatValue] >= [[version_code stringByReplacingOccurrencesOfString:@"." withString:@""] floatValue] )
        {
            isUpdata = NO;
        }else
        {
            isUpdata = YES;
        }
        if ([Version isEqualToString:version_code]) {
            isUpdata = NO;
        }
        
        if (isUpdata) {
            if (isUpdate.integerValue==1) {
                 [weakSelf VersionupdateWithmessage:content openUrl:url confirm:@"马上升级" cancel:nil state:1];
            }else
            {
                 [weakSelf VersionupdateWithmessage:content openUrl:url confirm:@"马上升级" cancel:@"我再想想" state:2];
            }
            
        }
         [self stopLoadingAnimation];
    } failure:^(id responseObject, NSError *error) {
         [self stopLoadingAnimation];
    }];
  
}
- (void)VersionupdateWithmessage:(NSString *)message
                         openUrl:(NSString *)url
                         confirm:(NSString *)confirm
                          cancel:(NSString *)cancel
                           state:(NSInteger )state
{
    
    CheckVersionAlearView * aleartVC = [[CheckVersionAlearView alloc] initWithmessage:message  titleStr:@"版本更新" openUrl:url confirm:confirm cancel:cancel state:state RechargeBlock:^{
        
        if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:url]])
        {
            
            if (@available(iOS 10.0, *)) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{} completionHandler:nil];
            } else {
                // Fallback on earlier versions
            }
        }else
        {
            if (@available(iOS 10.0, *)) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{} completionHandler:nil];
            } else {
                // Fallback on earlier versions
            }
        }
    }];
    [aleartVC showLXAlertView];
}

@end
